package edu.sdwu.liqing.qasystem.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.google.common.collect.Lists;
import edu.sdwu.liqing.qasystem.entity.QaAnswer;
import edu.sdwu.liqing.qasystem.entity.QaAnswerComment;
import edu.sdwu.liqing.qasystem.entity.QaUser;
import edu.sdwu.liqing.qasystem.mapper.QaAnswerCommentMapper;
import edu.sdwu.liqing.qasystem.service.IQaAnswerCommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.sdwu.liqing.qasystem.service.IQaAnswerService;
import edu.sdwu.liqing.qasystem.service.IQaUserService;
import edu.sdwu.liqing.qasystem.util.OperateUtil;
import edu.sdwu.liqing.qasystem.util.RequestUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 问题回答评论表 entity&QASystem&qa_answer_comment 服务实现类
 * </p>
 *
 * @author liqing@sdwu.edu
 * @since 2020-03-22
 */
@Service
public class QaAnswerCommentServiceImpl extends ServiceImpl<QaAnswerCommentMapper, QaAnswerComment> implements IQaAnswerCommentService {

    @Autowired
    IQaAnswerService answerService;

    @Autowired
    OperateUtil operateUtil;

    @Autowired
    RequestUtil requestUtil;

    @Autowired
    IQaUserService userService;

    @Override
    @Transactional
    public synchronized boolean save(QaAnswerComment comment) {
        QaAnswer answer = answerService.get(comment.getAnswerId());
        if (Objects.isNull(answer)) {
            return false;
        }
        boolean flag = super.save(comment);
        if (flag) {
            answer.setCommentNum(answer.getCommentNum() + 1);
            answerService.updateById(answer);
        }
        return flag;
    }

    @Override
    public boolean update(QaAnswerComment comment) {
        if (StringUtils.isBlank(comment.getId())) {
            return false;
        }
        QaAnswerComment answerComment = super.getById(comment.getId());
        if (Objects.isNull(answerComment) || !operateUtil.canOperate(answerComment)) {
            //不可操作
            return false;
        }
        return super.updateById(comment);
    }

    @Override
    @Transactional
    public synchronized boolean delete(String id) {
        if (StringUtils.isBlank(id)) {
            return false;
        }
        QaAnswerComment comment = super.getById(id);
        if (Objects.isNull(comment) || !operateUtil.canOperate(comment)) {
            return false;
        }
        boolean flag = super.removeById(id);
        if (flag) {
            QaAnswer answer = answerService.get(comment.getAnswerId());
            if (Objects.nonNull(answer)) {
                answer.setCommentNum(answer.getCommentNum() - 1);
                if (answer.getCommentNum() < 0) {
                    answer.setCommentNum(0);
                }
                answerService.updateById(answer);
            }
        }
        return flag;
    }

    @Override
    public List<QaAnswerComment> list(String answerId) {
        if (StringUtils.isBlank(answerId)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        /**
         * 这里的获取用户 为了比对是否为当前用户所创建 表示标志位供前端使用
         * */
        QaUser user = requestUtil.getCurrentUser();
        QueryWrapper<QaAnswerComment> qw = Wrappers.query();
        qw.eq("answer_id", answerId);
        List<QaAnswerComment> commentList = super.list();
        if (CollectionUtils.isEmpty(commentList)) {
            return Lists.newArrayListWithExpectedSize(0);
        }
        List<QaUser> userList = userService.list();
        final List<QaUser> allUserList = CollectionUtils.isEmpty(userList) ? Lists.newArrayListWithExpectedSize(0): userList;
        commentList.parallelStream().forEach(i-> {
            i.setCanOperate(
                    StringUtils.containsIgnoreCase("ADMIN", user.getRoleName()) ||
                    StringUtils.equals(user.getId(), i.getCreatedBy())
                    );
            i.setCreatedName(
                    allUserList.parallelStream().filter(item -> StringUtils.equals(item.getId(), i.getCreatedBy())).findFirst().orElse(new QaUser()).getRealName()
            );
        });
        return commentList;
    }
}
